package com.kefu.call.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.kefu.call.model.IvrViews;
import com.kefu.call.model.IvrViewsType;
import com.kefu.call.service.IvrViewsService;
import com.kefu.call.vo.IvrViewsTypeSearchVo;
import com.kefu.call.service.IvrViewsTypeService;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags = "ivr模块节点")
@RestController
@RequestMapping("call/ivrViewsType")
@Slf4j
public class IvrViewsTypeController extends AbstractBaseController {
    @Resource
    private IvrViewsService ivrViewsService;
    @Resource
    private IvrViewsTypeService ivrViewsTypeService;

    @PostMapping("/list")
    @ApiOperation(value = "ivr模块节点列表")
    public Result<List<IvrViewsType>> list(@RequestBody IvrViewsTypeSearchVo search) {
        logger.info("ivr模板列表:{}", search);
        List<IvrViewsType> list = Lists.newArrayList();
        String comOid = search.getComOid();
        QueryWrapper<IvrViews> wrapper = Wrappers.query();
        wrapper.select("*");
        wrapper.eq("comoid", comOid);
        List<IvrViews> ivrViewsList = ivrViewsService.list(wrapper);
        if (CollUtil.isNotEmpty(ivrViewsList)) {
            list = ivrViewsTypeService.list(Wrappers.lambdaQuery(IvrViewsType.class)
                    .in(IvrViewsType::getViewsoid,
                            ivrViewsList.stream().map(IvrViews::getMid).collect(Collectors.toList())));
        }
        return  ResultGenerator.genSuccessResult(list);
    }

}
